import requests
import parsel
import csv

f = open('一级.csv', mode='a', encoding='utf-8', newline='')
csv_writer = csv.DictWriter(f, fieldnames=[
            '标题',
            '详情页'
])
csv_writer.writeheader()    # 写入表头
for page in range(1, 6):
    url = f'http://www.tfhk.edu.cn/News_lists_sort_83_p_{page}.shtml'
    resp = requests.get(url=url)
    selector = parsel.Selector(resp.text)
    detail_list = selector.css('div.rightbox > div.box2 > div.thenews > ul > li.wow > a::attr("href")').getall()
    # detail_list = selector.css('.box2 ul .animated a::attr(href)').getall()
    for item in detail_list:
        new_url = 'http://www.tfhk.edu.cn' + item
        # print(new_url)
        resp_1 = requests.get(url=new_url)
        selector = parsel.Selector(resp_1.text)
        title = selector.css('.box2 div.theTit::text').get().strip()
        dic = {
            '标题': title,
            '详情页': new_url
        }
        csv_writer.writerow(dic)
        print(title, new_url)







